package com.cskaoyan.order.service.impl;


import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @Date: 2022/6/18 11:39
 * @Description:订单查询实现类
 * @Author: 李宇浩
 * @return
 */
@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    // 显示用户订单信息
    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        // 在执行查询之前，可以开启分页
        // sql语句预编译的过程给你拼接上分页的sql
        PageHelper.startPage(request.getPage(), request.getSize(), request.getSort());

        OrderListResponse orderListResponse = new OrderListResponse();

        // 参数校验
        try {
            request.requestCheck();

            // 根据用户id查询出所有的订单号
            List<String> orderIds = orderMapper.selectOrderIdDescByUserId(request.getUserId(), request.getSort());

            // 开启分页
            PageInfo pageInfo = new PageInfo(orderIds);

            List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
            List<OrderItemDto> orderItemDtos = new ArrayList<>();
            OrderShippingDto orderShippingDto = new OrderShippingDto();
            // 根据订单号查询出相应的订单信息
            for (String orderId : orderIds) {
                OrderDetailInfo orderDetailInfo = new OrderDetailInfo();

                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
                OrderDto orderDto = orderMapper.selectOrderInfoByOrderId(orderId);

                orderDetailInfo.setOrderId(orderDto.getOrderId());
                orderDetailInfo.setPayment(orderDto.getPayment());
                orderDetailInfo.setPaymentType(orderDto.getPaymentType());
                orderDetailInfo.setPostFee(orderDto.getPostFee());
                orderDetailInfo.setStatus(orderDto.getStatus());
                orderDetailInfo.setCreateTime(orderDto.getCreateTime());
                orderDetailInfo.setUpdateTime(orderDto.getUpdateTime());
                orderDetailInfo.setPaymentTime(orderDto.getPaymentTime());
                orderDetailInfo.setConsignTime(orderDto.getConsignTime());
                orderDetailInfo.setEndTime(orderDto.getEndTime());
                orderDetailInfo.setCloseTime(orderDto.getCloseTime());
                orderDetailInfo.setShippingName(orderDto.getShippingName());
                orderDetailInfo.setShippingCode(orderDto.getShippingCode());
                orderDetailInfo.setUserId(orderDto.getUserId());
                orderDetailInfo.setBuyerMessage(orderDto.getBuyerMessage());
                orderDetailInfo.setBuyerNick(orderDto.getBuyerNick());
                orderDetailInfo.setBuyerComment(orderDto.getBuyerComment());

                orderItemDtos = orderConverter.item2dto(orderItems);
                orderDetailInfo.setOrderItemDto(orderItemDtos);


                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
                orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);

                orderDetailInfos.add(orderDetailInfo);
            }

            orderListResponse.setDetailInfoList(orderDetailInfos);
            orderListResponse.setTotal(pageInfo.getTotal());

            orderListResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            orderListResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(orderListResponse, e);
        }

        return orderListResponse;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        return null;
    }
}
